﻿using System.ServiceModel.Description;
using CoreEx.Common.Extensions;
using LinFu.IoC;
using LinFu.IoC.Configuration;

namespace CoreEx.ServiceModel.Implementation
{
    /// <summary>
    /// Initializes a contract operation by adding all registered <see cref="IOperationBehavior"/>
    /// implementations.
    /// </summary>
    [Implements(typeof(IInitializer<OperationDescription>), ServiceName = "OperationBehaviorInitializer")]
    public class OperationBehaviorInitializer : InitializerBase<OperationDescription>
    {
        /// <summary>
        /// Adds all registered <see cref="IOperationBehavior"/> implementations to 
        /// the <see cref="OperationDescription.Behaviors"/> collection.
        /// </summary>
        /// <param name="operationDescription">The target contract operation.</param>
        public override void Initialize(OperationDescription operationDescription)
        {
            var registeredOperationBehaviors = ServiceContainer.GetServices<IOperationBehavior>();
            registeredOperationBehaviors.ForEach(ob => operationDescription.Behaviors.Add(ob));                        
        }
    }
}
